﻿#include "nlocker.h"
#include "debug.h"
#include "sys_plat.h"

/**
 * @brief 锁相关的初始化
 */
net_err_t nlocker_init(nlocker_t *locker, nlocker_type_t type)
{
    if (NLOCKER_THREAD == type)
    {
        locker->mutex = sys_mutex_create();
        if (SYS_MUTEx_INVALID == locker->mutex)
        {
            DBG_ERROR(DBG_NLOCKER_LEVEL, "the mutex create failed!");
            return NET_ERR_SYS;
        }
    }

    locker->type = type;
    return NET_ERR_OK;
}

/**
 * @brief 销毁锁
 */
void nlocker_destroy(nlocker_t *locker)
{
    if (NLOCKER_THREAD == locker->type)
    {
        sys_mutex_free(locker->mutex);
    }
}

/**
 * @brief 上锁
 */
void nlocker_lock(nlocker_t *locker)
{
    if (NLOCKER_THREAD == locker->type)
    {
        sys_mutex_lock(locker->mutex);
    }
}

/**
 * @brief 解锁
 */
void nlocker_unlock(nlocker_t *locker)
{
    if (NLOCKER_THREAD == locker->type)
    {
        sys_mutex_unlock(locker->mutex);
    }
}